<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-octane" tabindex="-1"><a class="header-anchor" href="#laravel-octane"><span>Laravel Octane</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#server-prerequisites">服务器先决条件</a>
<ul>
<li><a href="#frankenphp">FrankenPHP</a></li>
<li><a href="#roadrunner">RoadRunner</a></li>
<li><a href="#swoole">Swoole</a></li>
</ul>
</li>
<li><a href="#serving-your-application">为你的应用提供服务</a>
<ul>
<li><a href="#serving-your-application-via-https">通过 HTTPS 为你的应用提供服务</a></li>
<li><a href="#serving-your-application-via-nginx">通过 Nginx 为你的应用提供服务</a></li>
<li><a href="#watching-for-file-changes">监视文件更改</a></li>
<li><a href="#specifying-the-worker-count">指定工作进程数</a></li>
<li><a href="#specifying-the-max-request-count">指定最大请求次数</a></li>
<li><a href="#reloading-the-workers">重新加载工作进程</a></li>
<li><a href="#stopping-the-server">停止服务器</a></li>
</ul>
</li>
<li><a href="#dependency-injection-and-octane">依赖注入和 Octane</a>
<ul>
<li><a href="#container-injection">容器注入</a></li>
<li><a href="#request-injection">请求注入</a></li>
<li><a href="#configuration-repository-injection">配置存储库注入</a></li>
</ul>
</li>
<li><a href="#managing-memory-leaks">管理内存泄漏</a></li>
<li><a href="#concurrent-tasks">并发任务</a></li>
<li><a href="#ticks-and-intervals">时钟周期和间隔</a></li>
<li><a href="#the-octane-cache">Octane 缓存</a></li>
<li><a href="#tables">Tables</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/octane" target="_blank" rel="noopener noreferrer">Laravel Octane</a> 通过使用高性能的应用服务器（包括 <a href="https://frankenphp.dev/" target="_blank" rel="noopener noreferrer">FrankenPHP</a>、<a href="https://openswoole.com/" target="_blank" rel="noopener noreferrer">Open Swoole</a>、<a href="https://github.com/swoole/swoole-src" target="_blank" rel="noopener noreferrer">Swoole</a> 和 <a href="https://roadrunner.dev/" target="_blank" rel="noopener noreferrer">RoadRunner</a>）来提升你的应用性能。Octane 会在内存中启动你的应用程序，并以超音速速度处理请求。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>可以通过 Composer 包管理器安装 Octane：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/octane</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>安装 Octane 后，可以执行 <code v-pre>octane:install</code> Artisan 命令，该命令会将 Octane 的配置文件安装到你的应用程序中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:install</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="服务器先决条件" tabindex="-1"><a class="header-anchor" href="#服务器先决条件"><span>服务器先决条件</span></a></h2>
<blockquote>
<p><strong>警告</strong><br>
Laravel Octane 需要 <a href="https://php.net/releases/" target="_blank" rel="noopener noreferrer">PHP 8.1+</a>。</p>
</blockquote>
<h3 id="frankenphp" tabindex="-1"><a class="header-anchor" href="#frankenphp"><span>FrankenPHP</span></a></h3>
<p><a href="https://frankenphp.dev/" target="_blank" rel="noopener noreferrer">FrankenPHP</a> 是一个用 Go 编写的 PHP 应用服务器，支持现代 Web 功能，如提前提示、Brotli 和 Zstandard 压缩。当你安装 Octane 并选择 FrankenPHP 作为你的服务器时，Octane 将自动为你下载并安装 FrankenPHP 二进制文件。</p>
<h4 id="通过-laravel-sail-使用-frankenphp" tabindex="-1"><a class="header-anchor" href="#通过-laravel-sail-使用-frankenphp"><span>通过 Laravel Sail 使用 FrankenPHP</span></a></h4>
<p>如果你计划使用 <a href="https://learnku.com/docs/laravel/11.x/sail" target="_blank" rel="noopener noreferrer">Laravel Sail</a> 开发你的应用程序，你应该运行以下命令来安装 Octane 和 FrankenPHP：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/sail up</span>
<span class="line"></span>
<span class="line">./vendor/bin/sail <span class="token function">composer</span> require laravel/octane</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你应该使用 <code v-pre>octane:install</code> Artisan 命令来安装 FrankenPHP 二进制文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/sail artisan octane:install <span class="token parameter variable">--server</span><span class="token operator">=</span>frankenphp</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>最后，在你的应用程序的 <code v-pre>docker-compose.yml</code> 文件中的 <code v-pre>laravel.test</code> 服务定义中添加一个 <code v-pre>SUPERVISOR_PHP_COMMAND</code> 环境变量。这个环境变量将包含 Sail 用来使用 Octane 为你的应用提供服务的命令，而不是使用 PHP 开发服务器：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">services:</span>
<span class="line">  laravel.test:</span>
<span class="line">    environment:</span>
<span class="line">      <span class="token key attr-name">SUPERVISOR_PHP_COMMAND: "/usr/bin/php -d variables_order</span><span class="token punctuation">=</span><span class="token value attr-value">EGPCS /var/www/html/artisan octane:start --server=frankenphp --host=0.0.0.0 --admin-port=2019 --port=80" # [tl! add]</span></span>
<span class="line">      XDG_CONFIG_HOME:  /var/www/html/config # [tl! add]</span>
<span class="line">      XDG_DATA_HOME:  /var/www/html/data # [tl! add]</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要启用 HTTPS、HTTP/2 和 HTTP/3，应用以下修改：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">services:</span>
<span class="line">  laravel.test:</span>
<span class="line">    ports:</span>
<span class="line">        - '${APP_PORT:-80}:80'</span>
<span class="line">        - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'</span>
<span class="line">        - '443:443' # [tl! add]</span>
<span class="line">        - '443:443/udp' # [tl! add]</span>
<span class="line">    environment:</span>
<span class="line">      <span class="token key attr-name">SUPERVISOR_PHP_COMMAND: "/usr/bin/php -d variables_order</span><span class="token punctuation">=</span><span class="token value attr-value">EGPCS /var/www/html/artisan octane:start --host=localhost --port=443 --admin-port=2019 --https" # [tl! add]</span></span>
<span class="line">      XDG_CONFIG_HOME:  /var/www/html/config # [tl! add]</span>
<span class="line">      XDG_DATA_HOME:  /var/www/html/data # [tl! add]</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常情况下，你应该通过 <code v-pre>https://localhost</code> 访问你的 FrankenPHP Sail 应用程序，因为使用 <code v-pre>https://127.0.0.1</code> 需要额外配置，并且是<a href="https://frankenphp.dev/docs/known-issues/#using-https127001-with-docker" target="_blank" rel="noopener noreferrer">不推荐的</a>。</p>
<h4 id="通过-docker-使用-frankenphp" tabindex="-1"><a class="header-anchor" href="#通过-docker-使用-frankenphp"><span>通过 Docker 使用 FrankenPHP</span></a></h4>
<p>使用 FrankenPHP 的官方 Docker 镜像可以提供更好的性能，并使用附带的额外扩展，这些扩展在静态安装的 FrankenPHP 中不包含。此外，官方 Docker 镜像支持在它本身不支持的平台上运行 FrankenPHP，比如 Windows。FrankenPHP 的官方 Docker 镜像适用于本地开发和生产用途。</p>
<p>你可以使用以下 Dockerfile 作为容器化你的 FrankenPHP 驱动的 Laravel 应用程序的起点：</p>
<div class="language-docker line-numbers-mode" data-highlighter="prismjs" data-ext="docker" data-title="docker"><pre v-pre class="language-docker"><code><span class="line"><span class="token instruction"><span class="token keyword">FROM</span> dunglas/frankenphp</span></span>
<span class="line"></span>
<span class="line"><span class="token instruction"><span class="token keyword">RUN</span> install-php-extensions <span class="token operator">\</span></span>
<span class="line">    pcntl</span></span>
<span class="line">    <span class="token comment"># 在这里添加其他 PHP 扩展...</span></span>
<span class="line"></span>
<span class="line"><span class="token instruction"><span class="token keyword">COPY</span> . /app</span></span>
<span class="line"></span>
<span class="line"><span class="token instruction"><span class="token keyword">ENTRYPOINT</span> [<span class="token string">"php"</span>, <span class="token string">"artisan"</span>, <span class="token string">"octane:frankenphp"</span>]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后，在开发过程中，你可以使用以下 Docker Compose 文件来运行你的应用程序：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token comment"># compose.yaml</span></span>
<span class="line">services:</span>
<span class="line">  frankenphp:</span>
<span class="line">    build:</span>
<span class="line">      context: .</span>
<span class="line">    <span class="token key attr-name">entrypoint: php artisan octane:frankenphp --max-requests</span><span class="token punctuation">=</span><span class="token value attr-value">1</span></span>
<span class="line">    ports:</span>
<span class="line">      - "8000:8000"</span>
<span class="line">    volumes:</span>
<span class="line">      - .:/app</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以查阅<a href="https://frankenphp.dev/docs/docker/" target="_blank" rel="noopener noreferrer">官方 FrankenPHP 文档</a>，了解更多关于使用 Docker 运行 FrankenPHP 的信息。</p>
<h3 id="roadrunner" tabindex="-1"><a class="header-anchor" href="#roadrunner"><span>RoadRunner</span></a></h3>
<p><a href="https://roadrunner.dev/" target="_blank" rel="noopener noreferrer">RoadRunner</a> 使用由 Go 构建的 RoadRunner 二进制文件。第一次启动基于 RoadRunner 的 Octane 服务器时，Octane 会提供下载和安装 RoadRunner 二进制文件的选项。</p>
<h4 id="通过-laravel-sail-使用-roadrunner" tabindex="-1"><a class="header-anchor" href="#通过-laravel-sail-使用-roadrunner"><span>通过 Laravel Sail 使用 RoadRunner</span></a></h4>
<p>如果你计划使用 <a href="https://learnku.com/docs/laravel/11.x/sail" target="_blank" rel="noopener noreferrer">Laravel Sail</a> 开发你的应用程序，你应该运行以下命令来安装 Octane 和 RoadRunner：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/sail up</span>
<span class="line"></span>
<span class="line">./vendor/bin/sail <span class="token function">composer</span> require laravel/octane spiral/roadrunner-cli spiral/roadrunner-http </span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你应该启动一个 Sail shell，并使用 <code v-pre>rr</code> 可执行文件来获取最新的基于 Linux 构建的 RoadRunner 二进制文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/sail shell</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 在 Sail shell 中...</span></span>
<span class="line">./vendor/bin/rr get-binary</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后，在你的应用程序的 <code v-pre>docker-compose.yml</code> 文件中的 <code v-pre>laravel.test</code> 服务定义中添加一个 <code v-pre>SUPERVISOR_PHP_COMMAND</code> 环境变量。这个环境变量将包含 Sail 用来使用 Octane 为你的应用提供服务的命令，而不是使用 PHP 开发服务器：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">services:</span>
<span class="line">  laravel.test:</span>
<span class="line">    environment:</span>
<span class="line">      <span class="token key attr-name">SUPERVISOR_PHP_COMMAND: "/usr/bin/php -d variables_order</span><span class="token punctuation">=</span><span class="token value attr-value">EGPCS /var/www/html/artisan octane:start --server=roadrunner --host=0.0.0.0 --rpc-port=6001 --port=80" # [tl! add]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后，确保 <code v-pre>rr</code> 可执行并构建你的 Sail 镜像：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">chmod</span> +x ./rr</span>
<span class="line"></span>
<span class="line">./vendor/bin/sail build --no-cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="swoole" tabindex="-1"><a class="header-anchor" href="#swoole"><span>Swoole</span></a></h3>
<p>如果你计划使用 Swoole 应用服务器为你的 Laravel Octane 应用提供服务，你必须安装 Swoole PHP 扩展。通常情况下，可以通过 PECL 完成：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">pecl <span class="token function">install</span> swoole</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="open-swoole" tabindex="-1"><a class="header-anchor" href="#open-swoole"><span>Open Swoole</span></a></h4>
<p>如果你想使用 Open Swoole 应用服务器为你的 Laravel Octane 应用提供服务，你必须安装 Open Swoole PHP 扩展。通常情况下，可以通过 PECL 完成：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">pecl <span class="token function">install</span> openswoole</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>使用 Laravel Octane 与 Open Swoole 提供了与 Swoole 相同的功能，如并发任务、时钟周期和间隔。</p>
<h4 id="通过-laravel-sail-使用-swoole" tabindex="-1"><a class="header-anchor" href="#通过-laravel-sail-使用-swoole"><span>通过 Laravel Sail 使用 Swoole</span></a></h4>
<blockquote>
<p><strong>警告</strong><br>
在通过 Sail 提供 Octane 应用服务之前，请确保你有最新版本的 Laravel Sail，并在应用程序的根目录中执行 <code v-pre>./vendor/bin/sail build --no-cache</code>。</p>
</blockquote>
<p>或者，你可以使用 <a href="https://learnku.com/docs/laravel/11.x/sail" target="_blank" rel="noopener noreferrer">Laravel Sail</a> 来开发基于 Swoole 的 Octane 应用程序，这是 Laravel 的官方基于 Docker 的开发环境。Laravel Sail 默认包含 Swoole 扩展。然而，你仍需要调整 Sail 使用的 <code v-pre>docker-compose.yml</code> 文件。</p>
<p>要开始，向你应用程序的 <code v-pre>docker-compose.yml</code> 文件中的 <code v-pre>laravel.test</code> 服务定义中添加一个 <code v-pre>SUPERVISOR_PHP_COMMAND</code> 环境变量。这个环境变量将包含 Sail 用来使用 Octane 为你的应用提供服务的命令，而不是使用 PHP 开发服务器：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">services:</span>
<span class="line">  laravel.test:</span>
<span class="line">    environment:</span>
<span class="line">      <span class="token key attr-name">SUPERVISOR_PHP_COMMAND: "/usr/bin/php -d variables_order</span><span class="token punctuation">=</span><span class="token value attr-value">EGPCS /var/www/html/artisan octane:start --server=swoole --host=0.0.0.0 --port=80" # [tl! add]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后，构建你的 Sail 镜像：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/sail build --no-cache</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="swoole-配置" tabindex="-1"><a class="header-anchor" href="#swoole-配置"><span>Swoole 配置</span></a></h4>
<p>Swoole 支持一些额外的配置选项，如果需要，你可以将它们添加到你的 <code v-pre>octane</code> 配置文件中。由于它们很少需要被修改，这些选项没有包含在默认配置文件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'swoole'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'log_file'</span> <span class="token operator">=></span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'logs/swoole_http.log'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'package_max_length'</span> <span class="token operator">=></span> <span class="token number">10</span> <span class="token operator">*</span> <span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="为你的应用提供服务" tabindex="-1"><a class="header-anchor" href="#为你的应用提供服务"><span>为你的应用提供服务</span></a></h2>
<p>Octane 服务器可以通过 <code v-pre>octane:start</code> Artisan 命令启动。默认情况下，此命令将使用你应用程序的 <code v-pre>octane</code> 配置文件中指定的服务器：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:start</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，Octane 将在端口 8000 上启动服务器，所以你可以通过 <code v-pre>http://localhost:8000</code> 在 Web 浏览器中访问你的应用程序。</p>
<h3 id="通过-https-提供你的应用程序" tabindex="-1"><a class="header-anchor" href="#通过-https-提供你的应用程序"><span>通过 HTTPS 提供你的应用程序</span></a></h3>
<p>默认情况下，通过 Octane 运行的应用程序生成以 <code v-pre>http://</code> 为前缀的链接。在通过 HTTPS 提供你的应用程序时，可以在你应用程序的 <code v-pre>config/octane.php</code> 配置文件中使用 <code v-pre>OCTANE_HTTPS</code> 环境变量，设置为 <code v-pre>true</code>。当这个配置值设置为 <code v-pre>true</code> 时，Octane 将指示 Laravel 使用 <code v-pre>https://</code> 为所有生成的链接添加前缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'https'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'OCTANE_HTTPS'</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="通过-nginx-为你的应用提供服务" tabindex="-1"><a class="header-anchor" href="#通过-nginx-为你的应用提供服务"><span>通过 Nginx 为你的应用提供服务</span></a></h3>
<blockquote>
<p><strong>注意</strong><br>
如果你还没有准备好管理自己的服务器配置，或者不熟悉配置运行强大的 Laravel Octane 应用所需的各种服务，请查看<a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a>。</p>
</blockquote>
<p>在生产环境中，你应该将你的 Octane 应用程序放在传统的 Web 服务器（如 Nginx 或 Apache）后面。这样做将允许 Web 服务器提供你的静态资源，例如图片和样式表，以及管理你的 SSL 证书终止。</p>
<p>在下面的 Nginx 配置示例中，Nginx 将提供站点的静态资源并代理请求到运行在端口 8000 上的 Octane 服务器：</p>
<div class="language-nginx line-numbers-mode" data-highlighter="prismjs" data-ext="nginx" data-title="nginx"><pre v-pre class="language-nginx"><code><span class="line"><span class="token directive"><span class="token keyword">map</span> <span class="token variable">$http_upgrade</span> <span class="token variable">$connection_upgrade</span></span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">default</span> upgrade</span><span class="token punctuation">;</span></span>
<span class="line">    ''      <span class="token directive"><span class="token keyword">close</span></span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">listen</span> <span class="token number">80</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">listen</span> [::]:80</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">server_name</span> domain.com</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">server_tokens</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">root</span> /home/forge/domain.com/public</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">index</span> index.php</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">charset</span> utf-8</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> /index.php</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">try_files</span> /not_exists @octane</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">try_files</span> <span class="token variable">$uri</span> <span class="token variable">$uri</span>/ @octane</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> = /favicon.ico</span> <span class="token punctuation">{</span> <span class="token directive"><span class="token keyword">access_log</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token directive"><span class="token keyword">log_not_found</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> = /robots.txt</span>  <span class="token punctuation">{</span> <span class="token directive"><span class="token keyword">access_log</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token directive"><span class="token keyword">log_not_found</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span> <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">access_log</span> <span class="token boolean">off</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token directive"><span class="token keyword">error_log</span>  /var/log/nginx/domain.com-error.log error</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">error_page</span> <span class="token number">404</span> /index.php</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token directive"><span class="token keyword">location</span> @octane</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">set</span> <span class="token variable">$suffix</span> <span class="token string">""</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token directive"><span class="token keyword">if</span> (<span class="token variable">$uri</span> = /index.php)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token directive"><span class="token keyword">set</span> <span class="token variable">$suffix</span> ?<span class="token variable">$query_string</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_http_version</span> 1.1</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Host <span class="token variable">$http_host</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Scheme <span class="token variable">$scheme</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> SERVER_PORT <span class="token variable">$server_port</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> REMOTE_ADDR <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> X-Forwarded-For <span class="token variable">$proxy_add_x_forwarded_for</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Upgrade <span class="token variable">$http_upgrade</span></span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_set_header</span> Connection <span class="token variable">$connection_upgrade</span></span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token directive"><span class="token keyword">proxy_pass</span> http://127.0.0.1:8000<span class="token variable">$suffix</span></span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="监视文件更改" tabindex="-1"><a class="header-anchor" href="#监视文件更改"><span>监视文件更改</span></a></h3>
<p>由于在 Octane 服务器启动时一次性加载你的应用程序到内存中，所以当你刷新浏览器时，对应用程序文件的任何更改都不会反映出来。例如，添加到你的 <code v-pre>routes/web.php</code> 文件的路由定义在服务器重新启动之前不会反映出来。为了方便起见，你可以使用 <code v-pre>--watch</code> 标志指示 Octane 在应用程序内部的任何文件更改时自动重新启动服务器：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:start <span class="token parameter variable">--watch</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在使用此功能之前，你应该确保 <a href="https://nodejs.org/" target="_blank" rel="noopener noreferrer">Node</a> 已安装在你的本地开发环境中。此外，你应该在你的项目中安装 <a href="https://github.com/paulmillr/chokidar" target="_blank" rel="noopener noreferrer">Chokidar</a> 文件监视库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">npm</span> <span class="token function">install</span> --save-dev chokidar</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用应用程序的 <code v-pre>config/octane.php</code> 配置文件中的 <code v-pre>watch</code> 配置选项来配置应该监视的目录和文件。</p>
<h3 id="指定工作进程数量" tabindex="-1"><a class="header-anchor" href="#指定工作进程数量"><span>指定工作进程数量</span></a></h3>
<p>默认情况下，Octane 将为你的机器提供的每个 CPU 核心启动一个应用程序请求工作进程。然后，这些工作进程将用于在进入应用程序时为传入的 HTTP 请求提供服务。当调用 <code v-pre>octane:start</code> 命令时，你可以手动指定要启动多少个工作进程，使用 <code v-pre>--workers</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:start <span class="token parameter variable">--workers</span><span class="token operator">=</span><span class="token number">4</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你正在使用 Swoole 应用服务器，还可以指定你希望启动多少个 <a href="#concurrent-tasks">任务工作进程</a>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:start <span class="token parameter variable">--workers</span><span class="token operator">=</span><span class="token number">4</span> --task-workers<span class="token operator">=</span><span class="token number">6</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="指定最大请求计数" tabindex="-1"><a class="header-anchor" href="#指定最大请求计数"><span>指定最大请求计数</span></a></h3>
<p>为了帮助防止内存泄漏，Octane 在处理 500 个请求后会优雅地重新启动任何工作进程。要调整此数字，你可以使用 <code v-pre>--max-requests</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:start --max-requests<span class="token operator">=</span><span class="token number">250</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="重新加载工作进程" tabindex="-1"><a class="header-anchor" href="#重新加载工作进程"><span>重新加载工作进程</span></a></h3>
<p>你可以使用 <code v-pre>octane:reload</code> 命令优雅地重新启动 Octane 服务器的应用程序工作进程。通常，在部署后应该执行此操作，以便你的新部署代码被加载到内存中，并用于为后续请求提供服务：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:reload</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="停止服务器" tabindex="-1"><a class="header-anchor" href="#停止服务器"><span>停止服务器</span></a></h3>
<p>你可以使用 <code v-pre>octane:stop</code> Artisan 命令停止 Octane 服务器：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:stop</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="检查服务器状态" tabindex="-1"><a class="header-anchor" href="#检查服务器状态"><span>检查服务器状态</span></a></h4>
<p>你可以使用 <code v-pre>octane:status</code> Artisan 命令检查 Octane 服务器的当前状态：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:status</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="依赖注入与-octane" tabindex="-1"><a class="header-anchor" href="#依赖注入与-octane"><span>依赖注入与 Octane</span></a></h2>
<p>由于 Octane 一次启动你的应用程序并将其保存在内存中，同时为请求提供服务，因此在构建应用程序时应考虑一些注意事项。例如，当请求工作进程初始启动时，你的应用程序服务提供者的 <code v-pre>register</code> 和 <code v-pre>boot</code> 方法只会执行一次。在后续请求中，将重用相同的应用程序实例。</p>
<p>因此，在将应用程序服务容器或请求注入到任何对象的构造函数时，你应特别小心。这样做可能会导致在后续请求中，该对象使用的容器或请求版本过时。</p>
<p>Octane 将自动处理在请求之间重置任何第一方框架状态。但是，Octane 并不总是知道如何重置你的应用程序创建的全局状态。因此，你应该知道如何以符合 Octane 的方式构建应用程序。接下来，我们将讨论在使用 Octane 时可能会导致问题的最常见情况。</p>
<h3 id="容器注入" tabindex="-1"><a class="header-anchor" href="#容器注入"><span>容器注入</span></a></h3>
<p>一般来说，你应该避免将应用程序服务容器或 HTTP 请求实例注入到其他对象的构造函数中。例如，以下绑定将整个应用程序服务容器注入到一个被绑定为单例的对象中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Service</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 注册任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">singleton</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，如果在应用程序启动过程中解析 <code v-pre>Service</code> 实例，容器将被注入到服务中，并且该容器将在后续请求中由 <code v-pre>Service</code> 实例持有。对于你的特定应用程序，这<strong>可能</strong>不是问题；但是，它可能导致容器意外地缺少后续请求中添加的绑定或在启动周期后由后续请求添加的绑定。</p>
<p>作为解决方法，你可以停止将绑定注册为单例，或者你可以将一个容器解析器闭包注入到服务中，始终解析当前容器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Service</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Container<span class="token punctuation">\</span>Container</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">singleton</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">Container</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>全局的 <code v-pre>app</code> 助手和 <code v-pre>Container::getInstance()</code> 方法将始终返回应用程序容器的最新版本。</p>
<h3 id="请求注入" tabindex="-1"><a class="header-anchor" href="#请求注入"><span>请求注入</span></a></h3>
<p>一般来说，你应该避免将应用程序服务容器或 HTTP 请求实例注入到其他对象的构造函数中。例如，以下绑定将整个请求实例注入到一个被绑定为单例的对象中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Service</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 注册任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">singleton</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'request'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，如果在应用程序启动过程中解析 <code v-pre>Service</code> 实例，HTTP 请求将被注入到服务中，并且该请求将在后续请求中由 <code v-pre>Service</code> 实例持有。因此，所有标头、输入和查询字符串数据都将不正确，以及所有其他请求数据。</p>
<p>作为解决方法，你可以停止将绑定注册为单例，或者你可以将一个请求解析器闭包注入到服务中，始终解析当前请求实例。或者，最推荐的方法是在运行时将对象需要的特定请求信息传递给对象的某个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Service</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'request'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">singleton</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$app</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'request'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 或者...</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$service</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>全局的 <code v-pre>request</code> 助手将始终返回应用程序当前处理的请求，因此在应用程序中使用它是安全的。</p>
<blockquote>
<p><strong>警告</strong><br>
在控制器方法和路由闭包中对 <code v-pre>Illuminate\Http\Request</code> 实例进行类型提示是可以接受的。</p>
</blockquote>
<h3 id="配置存储库注入" tabindex="-1"><a class="header-anchor" href="#配置存储库注入"><span>配置存储库注入</span></a></h3>
<p>一般来说，你应该避免将配置存储库实例注入到其他对象的构造函数中。例如，以下绑定将配置存储库注入到一个被绑定为单例的对象中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Service</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 注册任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">singleton</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'config'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，如果配置数值在请求之间发生变化，那么该服务将无法访问新值，因为它依赖于原始存储库实例。</p>
<p>作为解决方法，你可以停止将绑定注册为单例，或者你可以向类注入一个配置存储库解析器闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Service</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Container<span class="token punctuation">\</span>Container</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'config'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">singleton</span><span class="token punctuation">(</span><span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Service</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">Container</span><span class="token operator">::</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'config'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>全局的 <code v-pre>config</code> 将始终返回配置存储库的最新版本，因此在应用程序中使用它是安全的。</p>
<h3 id="管理内存泄漏" tabindex="-1"><a class="header-anchor" href="#管理内存泄漏"><span>管理内存泄漏</span></a></h3>
<p>请记住，Octane 在请求之间保持应用程序在内存中；因此，向静态维护的数组添加数据将导致内存泄漏。例如，以下控制器存在内存泄漏，因为对应用程序的每个请求将继续向静态的 <code v-pre>$data</code> 数组添加数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Service</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理传入请求。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">index</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Service</span><span class="token operator">::</span><span class="token variable">$data</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在构建应用程序时，你应特别注意避免创建这类内存泄漏。建议在本地开发过程中监控应用程序的内存使用情况，以确保不会引入新的内存泄漏到应用程序中。</p>
<h2 id="并发任务" tabindex="-1"><a class="header-anchor" href="#并发任务"><span>并发任务</span></a></h2>
<blockquote>
<p><strong>警告</strong><br>
此功能需要 <a href="#swoole">Swoole</a>。</p>
</blockquote>
<p>在使用 Swoole 时，你可以通过轻量级的后台任务并发执行操作。你可以使用 Octane 的 <code v-pre>concurrently</code> 方法实现这一点。你可以将此方法与 PHP 数组解构结合使用，以检索每个操作的结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Server</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Octane<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Octane</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span><span class="token variable">$users</span><span class="token punctuation">,</span> <span class="token variable">$servers</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Octane</span><span class="token operator">::</span><span class="token function">concurrently</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">Server</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由 Octane 处理的并发任务利用了 Swoole 的 &quot;task workers&quot;，并在与传入请求完全不同的进程中执行。用于处理并发任务的工作进程数量由 <code v-pre>octane:start</code> 命令中的 <code v-pre>--task-workers</code> 指令确定：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan octane:start <span class="token parameter variable">--workers</span><span class="token operator">=</span><span class="token number">4</span> --task-workers<span class="token operator">=</span><span class="token number">6</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在调用 <code v-pre>concurrently</code> 方法时，由于 Swoole 任务系统施加的限制，不应提供超过 1024 个任务。</p>
<h2 id="ticks-和-intervals" tabindex="-1"><a class="header-anchor" href="#ticks-和-intervals"><span>Ticks 和 Intervals</span></a></h2>
<blockquote>
<p><strong>警告</strong><br>
此功能需要 <a href="#swoole">Swoole</a>。</p>
</blockquote>
<p>在使用 Swoole 时，你可以注册每隔指定秒数执行的 &quot;tick&quot; 操作。你可以通过 <code v-pre>tick</code> 方法注册 &quot;tick&quot; 回调。提供给 <code v-pre>tick</code> 方法的第一个参数应该是表示 tick 名称的字符串。第二个参数应该是在指定间隔调用的可调用函数。</p>
<p>在这个例子中，我们将注册一个闭包，每 10 秒调用一次。通常，<code v-pre>tick</code> 方法应该在应用程序的某个服务提供者的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Octane</span><span class="token operator">::</span><span class="token function">tick</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'simple-ticker'</span><span class="token punctuation">,</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">ray</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Ticking...'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 <code v-pre>immediate</code> 方法，你可以指示 Octane 在 Octane 服务器初始启动时立即调用 tick 回调，并在此后每 N 秒调用一次：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Octane</span><span class="token operator">::</span><span class="token function">tick</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'simple-ticker'</span><span class="token punctuation">,</span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">ray</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Ticking...'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">immediate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="octane-缓存" tabindex="-1"><a class="header-anchor" href="#octane-缓存"><span>Octane 缓存</span></a></h2>
<blockquote>
<p><strong>警告</strong><br>
此功能需要 <a href="#swoole">Swoole</a>。</p>
</blockquote>
<p>在使用 Swoole 时，你可以利用 Octane 缓存驱动程序，它提供每秒高达 200 万次的读写速度。因此，对于需要从缓存层获得极致读/写速度的应用程序来说，这个缓存驱动是一个绝佳选择。</p>
<p>这个缓存驱动由 <a href="https://www.swoole.co.uk/docs/modules/swoole-table" target="_blank" rel="noopener noreferrer">Swoole 表</a> 提供支持。缓存中存储的所有数据对服务器上的所有工作进程都是可用的。然而，在服务器重新启动时，缓存数据将被清空：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Cache</span><span class="token operator">::</span><span class="token function">store</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'octane'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'framework'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
可在应用程序的 <code v-pre>octane</code> 配置文件中定义 Octane 缓存允许的最大条目数。</p>
</blockquote>
<h3 id="缓存间隔" tabindex="-1"><a class="header-anchor" href="#缓存间隔"><span>缓存间隔</span></a></h3>
<p>除了 Laravel 缓存系统提供的典型方法之外，Octane 缓存驱动还提供基于间隔的缓存。这些缓存会在指定的间隔自动刷新，应该在应用程序的某个服务提供者的 <code v-pre>boot</code> 方法中注册。例如，以下缓存将每五秒刷新一次：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Cache</span><span class="token operator">::</span><span class="token function">store</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'octane'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">interval</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'random'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token argument-name">seconds</span><span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="表" tabindex="-1"><a class="header-anchor" href="#表"><span>表</span></a></h2>
<blockquote>
<p><strong>警告</strong><br>
此功能需要 <a href="#swoole">Swoole</a>。</p>
</blockquote>
<p>在使用 Swoole 时，你可以定义并与自己的任意 <a href="https://www.swoole.co.uk/docs/modules/swoole-table" target="_blank" rel="noopener noreferrer">Swoole 表</a> 交互。Swoole 表提供极高的性能吞吐量，这些表中的数据可以被服务器上的所有工作进程访问。然而，在服务器重新启动时，其中的数据将会丢失。</p>
<p>表应该在应用程序的 <code v-pre>octane</code> 配置文件的 <code v-pre>tables</code> 配置数组中定义。已经为你配置了一个示例表，允许最多有 1000 行。可以通过在列类型后指定列大小来配置字符串列的最大大小，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'tables'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'example:1000'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'string:1000'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'int'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要访问表，你可以使用 <code v-pre>Octane::table</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Octane<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Octane</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Octane</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'uuid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Nuno Maduro'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'votes'</span> <span class="token operator">=></span> <span class="token number">1000</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Octane</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'uuid'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
Swoole 表支持的列类型有：<code v-pre>string</code>、<code v-pre>int</code> 和 <code v-pre>float</code>。</p>
</blockquote>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/octanemd/16723" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/oc...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/octanemd/16723" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/oc...</a></p>
</blockquote>
</div></template>


